package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.framework.common.*;
import com.yzkj.cpjxc.business.framework.utils.CollectionHelp;
import com.yzkj.cpjxc.business.framework.utils.StringHelp;
import com.yzkj.cpjxc.business.service.SysAuthorityService;
import com.yzkj.cpjxc.business.domain.SysRole;
import com.yzkj.cpjxc.business.domain.SysRoleQueryVo;
import com.yzkj.cpjxc.business.service.SysRoleService;
import com.yzkj.cpjxc.business.domain.SysUser;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 角色控制类
 */
@Controller
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysAuthorityService sysAuthorityService;

	@RequestMapping("/list") 
	@DoLog(cnContent = "查看系统角色列表信息", value = false) 
	@Permission(name = "sysRole.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("sysRole", sysRole);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/securityMgr/sysRole/list").addAllObjects(modelMap);
	}

	
	@RequestMapping("/ajaxPage") 
	@DoLog(cnContent = "系统角色列表ajax分页", value = false) 
	@Permission(name = "sysRole.view")
	public String ajaxPage(HttpServletRequest request, HttpServletResponse response, SysRoleQueryVo queryVo) throws Exception {
		try {
			//1、获取登录用户信息
			Pager<SysRole> p = new Pager<SysRole>();
			p.setCurrentPage(this.getCurrentPage(request));
			
			SysUser currentUser = (SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
			if(Constants.ADMIN.equals(currentUser.getUsername())){
				queryVo.setNotAdmin(false);
			}else{
				queryVo.setNotAdmin(true);
			}
			
			Pager<SysRole> pager = sysRoleService.search(p, queryVo);
			this.genPageJson(response, pager);
			return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	
	@RequestMapping("/toAdd") 
	@DoLog(cnContent = "去到添加系统角色页面", value = false) 
	@Permission(name = "sysRole.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("statusMap", ConstantParam.statusMap);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/securityMgr/sysRole/add").addAllObjects(modelMap);
	}

	
	@RequestMapping("/add") 
	@DoLog(cnContent = "添加系统角色", operationAction = "C") 
	@Permission(name = "sysRole.add")
	public void add(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		try {
			
			//过滤指定非法字符
			StringHelp.replaceFilteringChars(SysRole.class, sysRole);
			
			BusinessMap bm = sysRoleService.create(sysRole);
			this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	
	@RequestMapping("/toEdit") 
	@DoLog(cnContent = "去到编辑系统角色页面", value = false) 
	@Permission(name = "sysRole.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		//1、根据id查询明细
		SysRole result = sysRoleService.getById(sysRole.getRoleId());
		//2、组装数据
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("statusMap", ConstantParam.statusMap);
		modelMap.put("sysRole", result);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/securityMgr/sysRole/edit").addAllObjects(modelMap);
	}

	
	@RequestMapping("/edit") 
	@DoLog(cnContent = "编辑系统角色", operationAction = "U") 
	@Permission(name = "sysRole.edit")
	public void edit(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		try {
			
			//过滤指定非法字符
			StringHelp.replaceFilteringChars(SysRole.class, sysRole);
			
			BusinessMap bm = sysRoleService.edit(sysRole);
			this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	
	@RequestMapping("/view") 
	@DoLog(cnContent = "查看系统角色信息", value = false) 
	@Permission(name = "sysRole.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		SysRole result = sysRoleService.getById(sysRole.getRoleId());
		return new ModelAndView("sysMgr/securityMgr/sysRole/view").addObject("sysRole", result).addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	
	@RequestMapping("/delete") 
	@DoLog(cnContent = "删除系统角色信息", operationAction = "D") 
	@Permission(name = "sysRole.delete")
	public void delete(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		try {
			BusinessMap bm = sysRoleService.delete(sysRole.getRoleId());
			this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	
	@RequestMapping("/toSetAuthority") 
	@DoLog(cnContent = "去到添加权限页面", value = false) 
	@Permission(name = "sysRole.setAuthority")
	public ModelAndView toSetAuthority(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		SysRole result = sysRoleService.getById(sysRole.getRoleId());
		return new ModelAndView("sysMgr/securityMgr/sysRole/setAuthority").addObject("sysRole", result).addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	
	@RequestMapping("/setAuthority") 
	@DoLog(cnContent = "保存角色和权限的关系", operationAction = "C") 
	@Permission(name = "sysRole.setAuthority")
	public void setAuthority(HttpServletRequest request, HttpServletResponse response, SysRoleQueryVo queryVo) throws Exception {
		try {
			
			//不允许设置修改admin角色的权限信息
			SysRole sr = sysRoleService.getById(queryVo.getRoleId());
			if(sr != null && Constants.ADMIN.equals(sr.getRoleName())){
				this.ajax(response, "不允许设置修改系统admin角色的权限信息");
				return;
			}
			
			sysRoleService.setAuthority(queryVo.getRoleId(), queryVo.getAuthorityIds());
			this.ajax(response, Constants.SUCCESS);
		} catch (Exception e) {
			this.logException(e);
			this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	
	@RequestMapping("/relevanceUser") 
	@DoLog(cnContent = "查看角色关联用户页面", value = false) 
	@Permission(name = "sysRole.view")
	public ModelAndView relevanceUser(HttpServletRequest request, HttpServletResponse response, SysRole sysRole) throws Exception {
		//1、获取用户角色信息
		SysRole result = sysRoleService.getById(sysRole.getRoleId());
		//2、获取角色用户信息
		String userIds = "";
		List<SysUser> sysUserList = result.getSysUsers();
		if (CollectionHelp.isNotBank(sysUserList)) {
			for (SysUser su : sysUserList) {
				userIds = su.getUserId() + "," + userIds;
			}
		}
		//3、组装数据
		HashMap<String, Object> modelMap = new LinkedHashMap<String, Object>();
		modelMap.put("sysRole", result);
		modelMap.put("userIds", userIds);
		modelMap.put("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
		return new ModelAndView("sysMgr/securityMgr/sysRole/relevanceUser").addAllObjects(modelMap);
	}

}
